package ee.propaganda.gparrot.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.RootPanel;

import ee.propaganda.gparrot.client.app.AppServices;
import ee.propaganda.gparrot.client.app.WebApplication;
import ee.propaganda.gparrot.client.model.LoginInfo;
import ee.propaganda.gparrot.client.view.LoginPanel;


/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class Gparrot implements EntryPoint {
	
	/**
	 * This is the entry point method.
	 */
	public void onModuleLoad() {		
		final AppServices appServices = AppServices.getInstance();
		//get login information from server
		appServices.getAuthService().login(GWT.getHostPageBaseURL(), new AsyncCallback<LoginInfo>() { 
			public void onFailure(Throwable error) {
			}
			public void onSuccess(LoginInfo result) { 
				//if logged in start web application and provide references to an event bus and application services to it
				if(result.isLoggedIn()){
					
					appServices.setLoginInfo(result);
					new WebApplication(appServices);
				} else { //if not logged in, show login page
					RootPanel.get().add(new LoginPanel(result.getLoginUrl()));
				}
			}
		});
		
	}

}
